package damai.web;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class RabbitMQDemo {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "route1", durable = "true"),
            exchange = @Exchange(value = "directDemo",
                    type = ExchangeTypes.DIRECT),
            key = {"info", "user", "error"}))
    public void route1(String msg) {
        System.out.println("route1 : " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "route2"),
            exchange = @Exchange(value = "directDemo",
                    type = ExchangeTypes.DIRECT),
            key = {"error", "test"}))
    public void route2(String msg) {
        System.out.println("route2 : " + msg);
    }

    @Resource
    RabbitTemplate rabbitTemplate;

    @RequestMapping("send")
    public String send(String msg, String key) {
        rabbitTemplate.convertAndSend("directDemo", key, msg);
        return "消息发送成功! key=" + key + " , msg=" + msg;
    }


}
